package com.xmy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.study.entity.*;
import com.xmy.study.mapper.ChaPlanMapper;
import com.xmy.study.respons.ChaStudyPlan;
import com.xmy.study.service.IChaPlanService;
import com.xmy.study.service.IEduTaskService;
import com.xmy.study.service.IMqPushService;
import com.xmy.study.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ChaPlanServiceImpl extends ServiceImpl<ChaPlanMapper, ChaPlan> implements IChaPlanService {

    @Autowired
    @Lazy
    ChaPlanMapper chaPlanMapper;

    @Autowired
    @Lazy
    IEduTaskService iEduTaskService;

    @Autowired
    @Lazy
    IMqPushService iMqPushService;


    @Override
    public List<ChaPlan> getClearanceRateChaPlan(Wrapper wrapper) {
        return chaPlanMapper.getClearanceRateChaPlan(wrapper);
    }

    @Override
    public IPage<ChaPlan> getChaPanAddList(IPage<?> iPage,
                                           String schoolIdStr,
                                           String yearPart,
                                           String quarterNum,
                                           String teacherId,
                                           String gradeId,
                                           String yearClassId,
                                           String gradeSection,
                                           String classTypeId,
                                           String lessonNum,
                                           String status,
                                           String studentName,
                                           String subjectId,
                                           String count,
                                           Integer taskCategoryId) {
        Long courseVersionId = 1676874588501311488L;//暂时默认
        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneMax(yearPart, quarterNum, courseVersionId, subjectId, taskCategoryId);
        if (eduTaskNow == null) {
            return null;
        }

        Integer isHistory = 0;//1，当前加前面都未通过的
        if (lessonNum.equals("0")) {
            isHistory = 1;
            lessonNum = eduTaskNow.getTaskCourseNum().toString();
        }

        IPage<ChaPlan> chaPlanIPage = chaPlanMapper.getChaPanAddList(iPage, yearPart, quarterNum, schoolIdStr, teacherId, gradeId, yearClassId, classTypeId, lessonNum, isHistory, status, studentName, Long.parseLong(subjectId), courseVersionId, count, taskCategoryId);
        return chaPlanIPage;
    }

    @Override
    public List<ChaPlan> getPassRadeList(String yearPart,
                                         String quarterNum,
                                         String schoolIdStr,
                                         String yearClassIdStr,
                                         String type,
                                         String lessonNumStart,
                                         String lessonNum,
                                         String classTypeId,
                                         String readingType,
                                         String readingStatusStr,
                                         String isBeforQuarter,
                                         String passDate,
                                         String passDateStart,
                                         String passDateEnd,
                                         String subjectId,
                                         String rightNumStartDate,
                                         String rightNumEndDate,
                                         String trackId,
                                         Integer isYearClass,
                                         String radeType,
                                         String listState,
                                         String teacherId,
                                         String name,
                                         String classify,
                                         String trackType,
                                         String teamId,
                                         String include,
                                         String inType,
                                         Integer taskCategoryId,
                                         String isAchieve,
                                         String moldType,
                                         String courseSchedulingDetailName,
                                         String examSchedulingtType,
                                         String leadSchoolIds,
                                         String leadSubjectsIds,
                                         String leadYearClassIds,
                                         Long leadTeacherId,
                                         Integer isIncludeFirstCourse
    ) {
        Long courseVersionId = 1676874588501311488L;//暂时默认
        //当前到了第几课次
        String tastSubjectsIds = "";
        if (subjectId.equals("-1")){
            tastSubjectsIds = leadSubjectsIds;
        }else {
            tastSubjectsIds = subjectId+","+leadSubjectsIds;
        }

        EduTask eduTaskNow = iEduTaskService.getTaskOneNow(yearPart, quarterNum, courseVersionId, tastSubjectsIds, taskCategoryId);

        if (eduTaskNow == null) {
            eduTaskNow = iEduTaskService.getTaskOneBackTaskNum(yearPart, quarterNum, courseVersionId, tastSubjectsIds, taskCategoryId);
        }

        if (eduTaskNow == null) {
            return null;
        }

        String lessonNow = eduTaskNow.getTaskCourseNum().toString();

        String isAll = "0";//是否查看所有课次
        if (StringUtils.isBlank(lessonNum)) {
            isAll = "1";
        }

        if (StringUtils.isBlank(isAchieve)) {
            isAchieve = "0";
        }

        if (examSchedulingtType.equals("0")) {
            //examSchedulingtType为0 原来的

            if (type.equals("5")) {
                return chaPlanMapper.getPassRadeTeamList(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, classify, trackType, teamId, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId);
            } else {
                return chaPlanMapper.getPassRadeList(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, String.valueOf(isIncludeFirstCourse));
            }

        } else if (examSchedulingtType.equals("1")) {
            // 已过刷题排班时间
            LocalDate today = LocalDate.now();
            DayOfWeek todayOfWeek = today.getDayOfWeek();
            DayOfWeek monday = DayOfWeek.MONDAY;
            String schoolDateStart = null;
            String schoolDateEnd = null;

            if (todayOfWeek == DayOfWeek.MONDAY) {
                return null; // 如果今天是周一，返回空
            }

            LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(monday));
            LocalDate endOfWeek = today.minusDays(1); // 当前时间前一天
            // 已过刷题排班时间为周一到当前星期几的前一天
            schoolDateStart = startOfWeek.toString();
            schoolDateEnd = endOfWeek.toString();
            if (type.equals("5")) {
                return chaPlanMapper.getPassedBrushiTeamDate(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, classify, trackType, teamId, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName,schoolDateStart,schoolDateEnd,examSchedulingtType);

            } else{
                return chaPlanMapper.getPassedBrushiDate(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName, schoolDateStart, schoolDateEnd, examSchedulingtType, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, String.valueOf(isIncludeFirstCourse));
           }

        } else {
            // 未到刷题排班时间
            LocalDate today = LocalDate.now();
            DayOfWeek todayOfWeek = today.getDayOfWeek();
            String schoolDateStart = null;
            String schoolDateEnd = null;
            //如果今天是周日，则返回空数据
//            if (todayOfWeek == DayOfWeek.SUNDAY) {
//                System.err.println("今天是周日，返回空数据");
//                return null;
//            }
            LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            schoolDateStart = today.toString();
            schoolDateEnd = endOfWeek.toString();

            if (type.equals("5")) {
                return chaPlanMapper.getPassedBrushiTeamDate(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, classify, trackType, teamId, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName,schoolDateStart,schoolDateEnd,examSchedulingtType);
            }else {
                return chaPlanMapper.getPassedBrushiDate(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNum, classTypeId, readingType, lessonNow, isAll, passDate, passDateStart, passDateEnd, subjectId, isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, trackId, isYearClass, radeType, listState, teacherId, name, include, inType, taskCategoryId, isAchieve, moldType, courseSchedulingDetailName, schoolDateStart, schoolDateEnd, examSchedulingtType, leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, String.valueOf(isIncludeFirstCourse));
            }

        }

    }

    @Override
    public IPage<ChaPlan> getPassRadeDetail(IPage<?> iPage,
                                            String yearPart,
                                            String quarterNum,
                                            String schoolIdStr,
                                            String teacherId,
                                            String yearClassIdStr,
                                            String type,
                                            String gradeId,
                                            String lessonNumStart,
                                            String lessonNum,
                                            String classTypeId,
                                            String readingType,
                                            String readingStatusStr,
                                            String isBeforQuarter,
                                            String passType,
                                            String courseVersionId,
                                            String passDate,
                                            String passDateStart,
                                            String passDateEnd,
                                            String subjectsId,
                                            String rightNumStartDate,
                                            String rightNumEndDate,
                                            String studentName,
                                            String radeType,
                                            String listState,
                                            String classify,
                                            String trackType,
                                            String teamId,
                                            String include,
                                            String inType,
                                            Integer taskCategoryId,
                                            String courseSchedulingDetailName,
                                            String studentIdStr,
                                            String moldType,
                                            String examSchedulingtType,
                                            String leadSchoolIds,
                                            String leadSubjectsIds,
                                            String leadYearClassIds,
                                            Long leadTeacherId,
                                            Integer isIncludeFirstCourse
    ) {
        if (StringUtils.isBlank(courseVersionId)) {
            courseVersionId = "1676874588501311488";//暂时默认
        }

        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneNow(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        if (eduTaskNow == null) {
            //不存在就获取最大的课次
            eduTaskNow = iEduTaskService.getTaskOneBackTaskNum(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        }

        String lessonNow = "0";
        if (eduTaskNow != null) {
            eduTaskNow.getTaskCourseNum().toString();
        }

        String isAll = "0";//是否查看所有课次
        if (StringUtils.isBlank(lessonNum)) {
            isAll = "1";
        }

        if (examSchedulingtType.equals("0")) {
            if (type.equals("5")) {
                return chaPlanMapper.getPassRadeTeamDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, classify, trackType, teamId, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType,leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId);
            } else {
                return chaPlanMapper.getPassRadeDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType,  leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, String.valueOf(isIncludeFirstCourse));
            }
        }else if (examSchedulingtType.equals("1")) {
            // 已过刷题排班时间
            LocalDate today = LocalDate.now();
            DayOfWeek todayOfWeek = today.getDayOfWeek();
            DayOfWeek monday = DayOfWeek.MONDAY;
            String schoolDateStart = null;
            String schoolDateEnd = null;

            if (todayOfWeek == DayOfWeek.MONDAY) {
                return null; // 如果今天是周一，返回空
            }

            LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(monday));
            LocalDate endOfWeek = today.minusDays(1); // 当前时间前一天
            // 已过刷题排班时间为周一到当前星期几的前一天
            schoolDateStart = startOfWeek.toString();
            schoolDateEnd = endOfWeek.toString();

            if (StringUtils.isBlank(include)){
                include = null;
            }
            if (type.equals("5")) {
                return chaPlanMapper.getPassedBrushiTeamDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, classify, trackType, teamId, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType,schoolDateStart, schoolDateEnd, examSchedulingtType);
            }else {
                return chaPlanMapper.getPassedBrushiDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType, schoolDateStart, schoolDateEnd, examSchedulingtType,leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId, String.valueOf(isIncludeFirstCourse));
            }

        }else {
            // 未到刷题排班时间
            LocalDate today = LocalDate.now();
            DayOfWeek todayOfWeek = today.getDayOfWeek();
            String schoolDateStart = null;
            String schoolDateEnd = null;
            //如果今天是周日，则返回空数据
//            if (todayOfWeek == DayOfWeek.SUNDAY) {
//                return null;
//            }
            LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            schoolDateStart = today.toString();
            schoolDateEnd = endOfWeek.toString();
            if (StringUtils.isBlank(include)){
                include = null;
            }
            if (type.equals("5")) {
                return chaPlanMapper.getPassedBrushiTeamDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, classify, trackType, teamId, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType,schoolDateStart, schoolDateEnd, examSchedulingtType);
            }else {
                return chaPlanMapper.getPassedBrushiDetail(iPage, yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, listState, include, inType, taskCategoryId, courseSchedulingDetailName, studentIdStr, moldType, schoolDateStart, schoolDateEnd, examSchedulingtType,leadSchoolIds, leadSubjectsIds, leadYearClassIds, leadTeacherId,String.valueOf(isIncludeFirstCourse));
            }
        }

    }

    /**
     * 合计
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolIdStr
     * @param teacherId
     * @param yearClassIdStr
     * @param type
     * @param gradeId
     * @param lessonNumStart
     * @param lessonNum
     * @param classTypeId
     * @param readingStatusStr
     * @param isBeforQuarter
     * @param passType
     * @param courseVersionId
     * @param subjectsId
     * @return
     */
    @Override
    public ChaPlan getPassRadeDetailTotal(String yearPart,
                                          String quarterNum,
                                          String schoolIdStr,
                                          String teacherId,
                                          String yearClassIdStr,
                                          String type,
                                          String gradeId,
                                          String lessonNumStart,
                                          String lessonNum,
                                          String classTypeId,
                                          String readingType,
                                          String readingStatusStr,
                                          String isBeforQuarter,
                                          String passType,
                                          String courseVersionId,
                                          String passDate,
                                          String passDateStart,
                                          String passDateEnd,
                                          String subjectsId,
                                          String rightNumStartDate,
                                          String rightNumEndDate,
                                          String studentName,
                                          String radeType,
                                          String include,
                                          String inType,
                                          Integer taskCategoryId) {
        if (StringUtils.isBlank(courseVersionId)) {
            courseVersionId = "1676874588501311488";//暂时默认
        }

        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneNow(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        if (eduTaskNow == null) {
            //不存在就获取最大的课次
            eduTaskNow = iEduTaskService.getTaskOneBackTaskNum(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        }

        String lessonNow = eduTaskNow.getTaskCourseNum().toString();

        String isAll = "0";//是否查看所有课次
        if (StringUtils.isBlank(lessonNum)) {
            isAll = "1";
        }
        return chaPlanMapper.getPassRadeDetailTotal(yearPart, quarterNum, schoolIdStr, teacherId, yearClassIdStr, type, gradeId, lessonNumStart, lessonNum, classTypeId, readingType, passType, lessonNow, isAll, Long.parseLong(courseVersionId), passDate, passDateStart, passDateEnd, Long.parseLong(subjectsId), isBeforQuarter, readingStatusStr, rightNumStartDate, rightNumEndDate, studentName, radeType, include, inType, taskCategoryId);
    }


    @Override
    public List<ChaPlan> getPassRadeDetailForStudentId(String yearPart, String quarterNum, String studentId, String subjectsId, Integer taskCategoryId, String yetLessonNumStr) {
        String courseVersionId = "1676874588501311488";//暂时默认
        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneNow(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        if (eduTaskNow == null) {
            //不存在就获取最大的课次
            eduTaskNow = iEduTaskService.getTaskOneBackTaskNum(yearPart, quarterNum, Long.parseLong(courseVersionId), subjectsId, taskCategoryId);
        }
        String lessonNow = eduTaskNow.getTaskCourseNum().toString();
        return chaPlanMapper.getPassRadeDetailForStudentId(yearPart, quarterNum, studentId, courseVersionId, subjectsId, lessonNow, taskCategoryId, yetLessonNumStr);
    }

    @Override
    public boolean addChaPlan(ChaPlan chaPlan) {
        LocalDate monDayDate = DateUtil.getMonDay();
        LocalDate sunDayDate = DateUtil.getSunDay();
        if (chaPlan.getCourseVersionId() == null) {
            chaPlan.setCourseVersionId(1676874588501311488L);//暂时默认
        }
        Integer categoryId = 1;//默认为1
        if (chaPlan.getTaskCategoryId() == null) {
            chaPlan.setTaskCategoryId(categoryId);//课程分类：1正式，3考前冲刺
        } else {
            categoryId = chaPlan.getTaskCategoryId();
        }

        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneForDate(monDayDate.toString(), sunDayDate.toString(), chaPlan.getYearClassId().toString(), chaPlan.getClassTypeId(), chaPlan.getCourseVersionId(), chaPlan.getSubjectsId(), categoryId);
        //修改
//        Integer planNum = chaPlan.getRightNum() + chaPlan.getAmendNum();
        //完成
        if (eduTaskNow != null) {
            chaPlan.setLessonNumFinish(eduTaskNow.getTaskCourseNum());
        } else {
            Integer maxNum = iEduTaskService.getTaskNumForMax(chaPlan.getYearClassId().toString(), chaPlan.getClassTypeId(), chaPlan.getCourseVersionId(), chaPlan.getSubjectsId(), categoryId);
            chaPlan.setLessonNumFinish(maxNum);
        }

        EduTask eduTask = iEduTaskService.getTaskOneForTaskCourseNum(chaPlan.getLessonNum(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getCourseVersionId(), chaPlan.getSubjectsId(), chaPlan.getYearPart(), chaPlan.getQuarterNum(), categoryId);
        if (eduTask == null) {
            return false;
        }
        //用当前课次获取是否已经添加过
        //ChaPlan chaPlanInfo = chaPlanMapper.getLastOne(chaPlan.getYearPart(),chaPlan.getQuarterNum(),chaPlan.getStudentId(),chaPlan.getYearClassId(),chaPlan.getClassTypeId(),chaPlan.getLessonNum(),chaPlan.getCourseVersionId(),chaPlan.getSubjectsId());
        ChaPlan chaPlanInfo = chaPlanMapper.getOneForLessonNum(chaPlan.getYearPart(), chaPlan.getQuarterNum(), chaPlan.getStudentId(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getLessonNum(), chaPlan.getCourseVersionId(), chaPlan.getSubjectsId(), categoryId);


        /*Integer isFinish = 0;
        if (eduTask.getLowestShutnum()>0){
            if (( chaPlanInfo.getRightNum() / eduTask.getLowestShutnum() )* 100 >= eduTask.getPassRade()){
                isFinish = 1 ;
            }
        }*/

        if (chaPlanInfo != null) {
            /*if (eduTask.getLowestShutnum() > planNum) {
                //改所有的为0
                //chaPlanMapper.updateIsLast(chaPlan.getYearPart(),chaPlan.getQuarterNum(),chaPlan.getStudentId(),chaPlan.getYearClassId(),chaPlan.getLessonNum(),0);
                isFinish = 0;
            }
            //chaPlanInfo.setIsFinish(isFinish);
            chaPlanMapper.updateIsFinish(chaPlan.getYearPart(), chaPlan.getQuarterNum(), chaPlan.getStudentId(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getLessonNum(), isFinish, chaPlan.getCourseVersionId(), chaPlan.getSubjectsId());
            */
            chaPlanMapper.updateIsLast(chaPlan.getYearPart(), chaPlan.getQuarterNum(), chaPlan.getStudentId(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getLessonNum(), 0, chaPlan.getCourseVersionId(), chaPlan.getSubjectsId(), categoryId);

            //如果已添加的正确数+修正数>=应通关数，则需要添加一条数据，相当与复刷
            if (chaPlanInfo.getRightNum() + chaPlanInfo.getAmendNum() >= eduTask.getLowestShutnum()) {
                chaPlan.setLowestShutnum(eduTask.getLowestShutnum());
                chaPlan.setPassRade(eduTask.getPassRade());
                chaPlan.setIsLast(1);
                //chaPlan.setIsFinish(isFinish);
                chaPlan.setId(null);
                chaPlan.setAddDate(LocalDate.now());
                this.save(chaPlan);
                iMqPushService.useGoodsNum(chaPlan);
            } else {
                chaPlanInfo.setRightNum(chaPlan.getRightNum());
                chaPlanInfo.setAmendNum(chaPlan.getAmendNum());
                chaPlanInfo.setLowestShutnum(eduTask.getLowestShutnum());
                chaPlanInfo.setPassRade(eduTask.getPassRade());
                //chaPlanInfo.setIsFinish(isFinish);
                chaPlanInfo.setIsLast(1);

                // 判断是否已经通关
                if (chaPlan.getRightNum() + chaPlan.getAmendNum() >= eduTask.getLowestShutnum()) {
                    chaPlanInfo.setPassDateTime(LocalDateTime.now());
                }

                this.updateById(chaPlanInfo);
            }
        } else {
            //添加
            /*if (eduTask.getLowestShutnum() > planNum) {
                isFinish = 0;
            }
            chaPlanMapper.updateIsLast(chaPlan.getYearPart(), chaPlan.getQuarterNum(), chaPlan.getStudentId(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getLessonNum(), 0, chaPlan.getCourseVersionId(), chaPlan.getSubjectsId());
            chaPlan.setIsFinish(isFinish);*/
            chaPlan.setLowestShutnum(eduTask.getLowestShutnum());
            chaPlan.setPassRade(eduTask.getPassRade());
            chaPlan.setIsLast(1);
            chaPlan.setIsFirst(1);
            //chaPlan.setIsFinish(isFinish);
            chaPlan.setId(null);
            chaPlan.setAddDate(LocalDate.now());

            // 判断是否已经通关
            if (chaPlan.getRightNum() + chaPlan.getAmendNum() >= eduTask.getLowestShutnum()) {
                chaPlan.setPassDateTime(LocalDateTime.now());
            }

            this.save(chaPlan);
            iMqPushService.useGoodsNum(chaPlan);
            //chaPlanMapper.updateIsFinish(chaPlan.getYearPart(), chaPlan.getQuarterNum(), chaPlan.getStudentId(), chaPlan.getYearClassId(), chaPlan.getClassTypeId(), chaPlan.getLessonNum(), isFinish, chaPlan.getCourseVersionId(), chaPlan.getSubjectsId());
        }

        ChaStudyPlan chaStudy = new ChaStudyPlan();
        chaStudy.setYearPart(chaPlan.getYearPart());
        chaStudy.setQuarterNum(chaPlan.getQuarterNum());
        chaStudy.setStudentId(chaPlan.getStudentId());
        chaStudy.setSubjectsId(chaPlan.getSubjectsId());
        chaStudy.setLessonNum(chaPlan.getLessonNum());
        chaStudy.setLessonType(3);
        chaStudy.setStatus(1);
        chaStudy.setRightNum(chaPlan.getRightNum());
        chaStudy.setAmendNum(chaPlan.getAmendNum());
        chaStudy.setLowestShutnum(eduTask.getLowestShutnum());
        chaStudy.setPassRade(eduTask.getPassRade());
        chaStudy.setStartTaskDate(eduTask.getStartTaskDate());
        chaStudy.setEndTaskDate(eduTask.getTaskDate());
        iMqPushService.chaStudyPlanAdd(chaStudy);

        //正式
        if (eduTask.getTaskCategoryId().equals(1)) {
            //准备率学生分层使用
            iMqPushService.chaStudyPlanAccuracyAdd(chaStudy);
        }
        return true;
    }

    @Override
    public List<ChaPlan> getNeedOrder(Integer yearPart,
                                      Integer quarterNum,
                                      Long schoolId,
                                      Integer yearClassId,
                                      Long classTypeId,
                                      Long subjectsId,
                                      Integer lessonType,
                                      Long goodsTypeId,
                                      Integer startLessonNum,
                                      Integer endLessonNum,
                                      Integer taskCategoryId) {
        String schoolIdStr = null;
        if (yearClassId != null) {
            schoolIdStr = yearClassId.toString();
        }
        return chaPlanMapper.getNeedOrder(yearPart, quarterNum, schoolId, schoolIdStr, classTypeId, subjectsId, lessonType, goodsTypeId, startLessonNum, endLessonNum, taskCategoryId);
    }

    @Override
    public IPage<StoreData> getNeedOrderForGroup(
            IPage<?> page,
            Integer yearPart,
            Integer quarterNum,
            Long schoolId,
            Integer yearClassId,
            Long classTypeId,
            Long subjectsId,
            Integer lessonType,
            Long goodsTypeId,
            Integer startLessonNum,
            Integer endLessonNum,
            Integer taskCategoryId) {
        String schoolIdStr = null;
        if (yearClassId != null) {
            schoolIdStr = yearClassId.toString();
        }
        IPage<StoreData> storeDataIPage = chaPlanMapper.getNeedOrderForGroup(page, yearPart, quarterNum, schoolId, schoolIdStr, classTypeId, subjectsId, lessonType, goodsTypeId, startLessonNum, endLessonNum, taskCategoryId);

        for (StoreData storeData : storeDataIPage.getRecords()) {

        }

        return storeDataIPage;
    }

    @Override
    public String getChaPlanLessonNumString(ChaPlan chaPlan) {
        String lessonNumStr = chaPlanMapper.getChaPlanLessonNumString(chaPlan);
        if (lessonNumStr == null) {
            lessonNumStr = "";
        }
        return lessonNumStr;
    }

    @Override
    public List<ChaPlan> getHonorList(String yearPart, String quarterNum, String schoolIdStr, String yearClassIdStr, String type, String lessonNumStart, String lessonNumEnd, String classTypeId, String readingType, String subjectsId, String isBeforQuarter, String trackId, String teacherId, String name, String include, Integer taskCategoryId, String classify, String trackType, String teamId, String startDate, String endDate) {
        List<ChaPlan> chaPlanList = new ArrayList<>();
        if (type.equals("5")) {
            chaPlanList = chaPlanMapper.getHonorTeamList(yearPart, quarterNum, yearClassIdStr, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, teacherId, name, include, taskCategoryId, classify, trackType, teamId, startDate, endDate);
        } else {
            chaPlanList = chaPlanMapper.getHonorList(yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, trackId, teacherId, name, include, taskCategoryId, startDate, endDate);
        }

        return chaPlanList;
    }

    @Override
    public IPage<ChaPlan> getHonorDetails(IPage<?> iPage, String yearPart, String quarterNum, String schoolIdStr, String yearClassIdStr, String type, String lessonNumStart, String lessonNumEnd, String classTypeId, String readingType, String subjectsId, String isBeforQuarter, String trackId, String teacherId, String name, String include, Integer taskCategoryId, String passType, String classify, String trackType, String teamId, String gradeId, String startDate, String endDate) {

        IPage<ChaPlan> chaPlanIPage = null;
        if (type.equals("5")) {
            chaPlanIPage = chaPlanMapper.getHonorTeamDetails(iPage, yearPart, quarterNum, yearClassIdStr, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, teacherId, name, include, taskCategoryId, classify, trackType, teamId, gradeId, startDate, endDate);
        } else {
            chaPlanIPage = chaPlanMapper.getHonorDetails(iPage, yearPart, quarterNum, schoolIdStr, yearClassIdStr, type, lessonNumStart, lessonNumEnd, classTypeId, readingType, subjectsId, isBeforQuarter, trackId, teacherId, name, include, taskCategoryId, passType, gradeId, startDate, endDate);
        }

        return chaPlanIPage;
    }

    @Override
    public List<ChaPlan> getPassRadeIntegralList(String yearPart, String quarterNum, String schoolId, String teacherId, String yearClassId, Long subjectsId, String classTypeId, String readingType, Long courseVersionId, String isBeforQuarter, String include, String taskCategoryId, String classify, String trackType, String teamId, String lessonNumStart, String lessonNumEnd, String startDate, String endDate, String type, String inType, String listState, String moldType) {
        List<ChaPlan> chaPlanList = new ArrayList<>();
        if (type.equals("5")) {
            chaPlanList = chaPlanMapper.getPassRadeIntegralTeamList(yearPart, quarterNum, schoolId, teacherId, yearClassId, subjectsId, classTypeId, readingType, courseVersionId, isBeforQuarter, include, taskCategoryId, classify, trackType, teamId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, listState, moldType);
        } else {
            chaPlanList = chaPlanMapper.getPassRadeIntegralList(yearPart, quarterNum, schoolId, teacherId, yearClassId, subjectsId, classTypeId, readingType, courseVersionId, isBeforQuarter, include, taskCategoryId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, listState, moldType);
        }

        return chaPlanList;
    }

    @Override
    public IPage<ChaPlan> getPassRadeIntegralDetail(IPage<?> page, String yearPart, String quarterNum, String schoolId, String teacherId, String studentId, String yearClassId, Long subjectsId, String gradeId, String classTypeId, String readingType, Long courseVersionId, String isBeforQuarter, String studentName, String include, String taskCategoryId, String classify, String trackType, String teamId, String lessonNumStart, String lessonNumEnd, String startDate, String endDate, String type, String inType, String moldType, String listState) {
        IPage<ChaPlan> chaPlanIPage = null;
        if (type.equals("5")) {
            chaPlanIPage = chaPlanMapper.getPassRadeIntegralTeamDetail(page, yearPart, quarterNum, schoolId, teacherId, studentId, yearClassId, subjectsId, gradeId, classTypeId, readingType, courseVersionId, isBeforQuarter, studentName, include, taskCategoryId, classify, trackType, teamId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, moldType, listState);
        } else {
            chaPlanIPage = chaPlanMapper.getPassRadeIntegralDetail(page, yearPart, quarterNum, schoolId, teacherId, studentId, yearClassId, subjectsId, gradeId, classTypeId, readingType, courseVersionId, isBeforQuarter, studentName, include, taskCategoryId, lessonNumStart, lessonNumEnd, startDate, endDate, type, inType, moldType, listState);
        }

        return chaPlanIPage;
    }

    @Override
    public IPage<ChaPlan> getChaPanIntegralAddList(IPage<?> page, String yearPart, String quarterNum, String schoolId, String teacherId, String gradeId, String yearClassId, String classTypeId, String lessonNum, String studentName, Long subjectsId, Long courseVersionId, String brushCount, Integer taskCategoryId) {
        //当前到了第几课次
        EduTask eduTaskNow = iEduTaskService.getTaskOneMax(yearPart, quarterNum, courseVersionId, String.valueOf(subjectsId), taskCategoryId);
        if (eduTaskNow == null) {
            return null;
        }

        Integer isHistory = 0;//1，当前加前面都未通过的
//        if (lessonNum.equals("0")) {
//            isHistory = 1;
//            lessonNum = eduTaskNow.getTaskCourseNum().toString();
//        }
        return chaPlanMapper.getChaPanIntegralAddList(page, yearPart, quarterNum, schoolId, teacherId, gradeId, yearClassId, classTypeId, lessonNum, isHistory, studentName, subjectsId, courseVersionId, brushCount, taskCategoryId);
    }


    @Override
    public boolean addChaPanIntegra(ChaPlan chaPlan) {
        ChaPlan plan = chaPlanMapper.selectById(chaPlan.getId());
        plan.setIntegral(chaPlan.getIntegral());
        plan.setIntegralAddDateTime(LocalDateTime.now());
        chaPlanMapper.updateById(plan);
        return true;
    }

    @Async
    @Override
    public void updatePassLesson(Integer yearPart, Integer quarterNum){
        QueryWrapper<ChaPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter_num", quarterNum);
        queryWrapper.eq("is_last", 1);
        List<ChaPlan> list = this.list(queryWrapper);
        for (ChaPlan chaPlan : list) {
            ChaStudyPlan chaStudy = new ChaStudyPlan();
            chaStudy.setYearPart(chaPlan.getYearPart());
            chaStudy.setQuarterNum(chaPlan.getQuarterNum());
            chaStudy.setStudentId(chaPlan.getStudentId());
            chaStudy.setSubjectsId(chaPlan.getSubjectsId());
            chaStudy.setLessonNum(chaPlan.getLessonNum());
            chaStudy.setLessonType(3);
            chaStudy.setStatus(1);
            chaStudy.setRightNum(chaPlan.getRightNum());
            chaStudy.setAmendNum(chaPlan.getAmendNum());
            chaStudy.setLowestShutnum(chaPlan.getLowestShutnum());
            chaStudy.setPassRade(chaPlan.getPassRade());
            EduTask eduTask = iEduTaskService.getTaskOneForLessonNum(chaPlan.getYearPart(),chaPlan.getQuarterNum(),chaPlan.getYearClassId(),chaPlan.getCourseVersionId(),chaPlan.getSubjectsId(),chaPlan.getTaskCategoryId(),chaPlan.getLessonNum());
            if (!Objects.isNull(eduTask)){
                chaStudy.setStartTaskDate(eduTask.getStartTaskDate());
                chaStudy.setEndTaskDate(eduTask.getTaskDate());
            }
            iMqPushService.chaStudyPlanAdd(chaStudy);
        }
    }
}
